package com.yq.ylmall.product.controller;

import com.yq.ylmall.result.Result;
import com.yq.ylmall.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "商品图片处理接口")
@RequiredArgsConstructor
@RequestMapping("/admin/product/image")
public class FileController {

    private final MinioService minioService;

//    @Log(title = "文件上传", businessType = 0)
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        System.out.println("file = " + file);
        return Result.ok(minioService.upload(file));
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response) {
        minioService.download(filename, response);
    }

    @ApiOperation(value = "批量文件删除")
    @DeleteMapping("/remove")
    public Result<Object> remove(@RequestBody List<String> fileList) {
        fileList.forEach(minioService::remove);
        return Result.ok();
    }

}
